package com.example.qing.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qing.config.AuthAccess;
import com.example.qing.config.LogType;
import com.example.qing.config.MyLogs;
import com.example.qing.pojo.Notice;
import com.example.qing.pojo.Result;
import com.example.qing.service.NoticeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {




    @Resource
    NoticeService noticeService;



    @MyLogs(operation = "公告",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        noticeService.removeById(id);

        return Result.success();
    }




    @MyLogs(operation = "公告",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> ids){
        noticeService.removeByIds(ids);

        return Result.success();
    }

    @MyLogs(operation = "公告",type = LogType.ADD)
    @PostMapping("/add")
    public Result save(@RequestBody Notice notice){
        notice.setTime(DateUtil.now());
        noticeService.save(notice);
        return Result.success();
    }
    @GetMapping("/total")
    public Result total(){
        long count= noticeService.count();
        return Result.success(count);
    }


/*
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){

        notice notice=noticeService.getById(id);
        return Result.success(notice);
    }
*/

    @MyLogs(operation = "文章",type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return Result.success();
    }


    @GetMapping("/selectByPage")
    public Result adminList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam String title,
                            @RequestParam Integer permission){
        log.info("分页查询");
        QueryWrapper<Notice>queryWrapper=new QueryWrapper<Notice>();
        queryWrapper.like(StrUtil.isNotBlank(title),"name",title);
        if (permission!=2) {
            queryWrapper.like("open",permission);
        }
        queryWrapper.orderByDesc("id");
        Page<Notice>page=noticeService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }

    @AuthAccess
    @GetMapping("/select/recommend")
    public Result list(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        log.info("分页查询");
        QueryWrapper<Notice>queryWrapper=new QueryWrapper<Notice>();
        queryWrapper.eq("open",true);
        queryWrapper.orderByDesc("id");
        Page<Notice>page=noticeService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }

    @AuthAccess
    @GetMapping("/recommend")
    public Result recommend(){
        QueryWrapper<Notice>queryWrapper=new QueryWrapper<Notice>();
        queryWrapper.eq("open",true);
        queryWrapper.orderByDesc("id");
        List<Notice>notices=noticeService.list(queryWrapper);
        return Result.success(notices);
    }


}
